//
//  WmcDevNetworkUploadCell.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/6/25.
//

import UIKit

class WmcDevNetworkUploadCell: WmcBaseTableCell {
    // Internal

    override func makeUI() {
        super.makeUI()

        lineView.backgroundColor = .wmc_grayH19_F5F5F5

        contentView.addSubview(photoImageView)
        contentView.addSubview(lineView)
        contentView.addSubview(progressBarView)
        contentView.addSubview(progressLabel)
        contentView.addSubview(statusLabel)
        
        photoImageView.addSubview(idxLabel)

        photoImageView.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(80)
        }

        progressBarView.snp.makeConstraints { make in
            make.left.equalTo(photoImageView.snp.right).offset(20)
            make.right.equalTo(-30)
            make.height.equalTo(6)
            make.top.equalTo(30)
        }

        progressLabel.snp.makeConstraints { make in
            make.left.equalTo(progressBarView)
            make.top.equalTo(progressBarView.snp.bottom).offset(10)
        }

        statusLabel.snp.makeConstraints { make in
            make.left.equalTo(progressBarView)
            make.top.equalTo(progressLabel.snp.bottom).offset(10)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        idxLabel.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }
    }

    func bindModel(_ model: WmcUploadTestModel, idx: Int) {
        idxLabel.text = "\(idx + 1)"
        
        if let data = model.data {
            photoImageView.image = UIImage(data: data)
        }
        let value = String(format: "%.2f", model.progressValue * 100)
        progressLabel.text = "上传进度：\(value)%"
        progressBarView.setProgress(Float(model.progressValue), animated: true)
        
        switch model.status {
        case .uploading:
            statusLabel.text = "正在上传..."
        case .completion:
            statusLabel.text = "上传成功"
        case .failure:
            statusLabel.text = "上传失败"
        case .cancel:
            statusLabel.text = "已取消上传"
        default:
            statusLabel.text = ""
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    // Private

    private lazy var photoImageView: UIImageView = {
        let view = UIImageView()
        view.layer.cornerRadius = 8
        return view
    }()
    
    private lazy var idxLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 30, weight: .heavy)
        label.textColor = .wmc_white_FFFFFF
        label.textAlignment = .center
        return label
    }()

    private lazy var progressLabel: UILabel = {
        let label = UILabel()
        label.font = 13.wmc_font
        label.textColor = .wmc_grayH5_606066
        label.text = "0%"
        return label
    }()
    
    private lazy var statusLabel: UILabel = {
        let label = UILabel()
        label.font = 13.wmc_font
        label.textColor = .wmc_grayH5_606066
        return label
    }()

    private lazy var progressView: WmcCircleProgressView = {
        let view = WmcCircleProgressView()
        view.startAngle = -90
        view.clockwise = true
        view.gradientRotateSpeed = 2
        view.roundedCorners = true
        view.glowMode = .noGlow
        view.glowAmount = 0.9
        view.set(colors: .wmc_mainTheme)
        view.trackColor = .clear
        return view
    }()

    private lazy var progressBarView: UIProgressView = {
        let progressView = UIProgressView(progressViewStyle: .default)
        progressView.progress = 0.0
        progressView.progressTintColor = .wmc_green_E6F8E6
        progressView.trackTintColor = .wmc_grayH22_FFF5E7
        progressView.layer.cornerRadius = 8
        return progressView
    }()
}
